package com.otp.webapp.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.appfuse.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.otp.model.test.template.Attribute;
import com.otp.model.test.template.AttributeSet;
import com.otp.model.test.template.AttributeUsers;
import com.otp.model.test.template.AttributeValue;
import com.otp.service.instance.AttributeUsersManager;
import com.otp.service.instance.AttributeValueManager;
import com.otp.service.instance.User1Manager;
import com.otp.webapp.comparators.CompAttributeValuesByOrder;
import com.otp.webapp.comparators.CompAttributesByOrder;


@Scope("session")
@Component("demographics")

public class Demographics extends BasePage implements Serializable{
	private static final long serialVersionUID = -1141119853856863204L;

	private AttributeValueManager attributeValueManager;
	private AttributeUsersManager attributeUsersManager;
	private User1Manager user1Manager;

	private boolean submit = false;
	private String description = "";
	private String selection;
	private List<String> descriptions = new ArrayList<String>();
	private List<SelectMenuDemographics> selectMenus = new ArrayList<SelectMenuDemographics>();

	

	@Autowired
	public void setAttributeValueManager(@Qualifier("attributeValueManager") AttributeValueManager attributeValueManager) {
		this.attributeValueManager = attributeValueManager;
	}
	
	@Autowired
	public void setAttributeUsersManager(@Qualifier("attributeUsersManager") AttributeUsersManager attributeUsersManager) {
		this.attributeUsersManager = attributeUsersManager;
	}

	
	@Autowired
	public void setUser1Manager(@Qualifier("user1Manager") User1Manager user1Manager) {
		this.user1Manager = user1Manager;
	}

	public boolean isSubmit() {
		return submit;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<String> getDescriptions() {
		return descriptions;
	}

	public void setDescriptions(List<String> descriptions) {
		this.descriptions = descriptions;
	}

	public String getSelection() {
		return selection;
	}

	public void setSelection(String selection) {
		this.selection = selection;
	}

	public List<SelectMenuDemographics> getSelectMenus() {
		return selectMenus;
	}

	public void setSelectMenus(List<SelectMenuDemographics> selectMenus) {
		this.selectMenus = selectMenus;
	}

	public List<Attribute> getAttributes() {
		HttpServletRequest request = (HttpServletRequest) getFacesContext().getExternalContext().getRequest();
		//TODO move manager to BasePage
		User user = user1Manager.getUserByUsername(request.getRemoteUser());		
		List<Attribute> attributes = new ArrayList<Attribute>();
		AttributeSet set = user.getOrgs().getAttSet();
		if(set==null) attributes=null;
		else attributes=set.getAtts();
		if(attributes!=null) Collections.sort(attributes, new CompAttributesByOrder());
		return attributes;		
	}
	
	public String displayQuestions(){
		selectMenus.clear();
		List<Attribute> attributes = getAttributes();		
		for(Attribute att:attributes){
			List<AttributeValue> values = attributeValueManager.getAttributeValuesByParentAttribute(att.getObjectId()+"");
			Collections.sort(values, new CompAttributeValuesByOrder());
			SelectMenuDemographics menu = new SelectMenuDemographics(att, values);
			selectMenus.add(menu);
		}
		submit=false;
		return "demographics.xhtml";
	}

	public String save(){
		submit=false;
		HttpServletRequest request = (HttpServletRequest) getFacesContext().getExternalContext().getRequest();
		User user = user1Manager.getUserByUsername(request.getRemoteUser());
		for(SelectMenuDemographics menu:selectMenus){
			AttributeValue val=attributeValueManager.get(Long.parseLong(menu.getSelection()));
			AttributeUsers attToSave = new AttributeUsers();
			attToSave.setAttribute(menu.getAttribute());
			attToSave.setAttributeValue(val);
			attToSave.setUser(user);
			user.setDemoDone(true);
			attributeUsersManager.save(attToSave);
			user1Manager.save(user);
		}
		submit=true;
		addMessage("demographics.complete");
		return null;
	}	
	
	public String cancel(){
		return "mainMenu.xhtml";
	}
}
